import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import init from '@feidao/web/framework/browser/dn/component';
import s from './s';
import P01 from './P01';
import P02 from './P02';
import P03 from './P03';
import P04 from './P04';
import nodejs from '@feidao/web/atom/browser/msg/nodejs';
import render from '@feidao/web/atom/browser/render/render';
import { query2obj, generate } from '@feidao/core/querystring';
const feidao = {};

const daoke = {
	async init(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 init b', args);
	},
	async eu01(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 change 教学楼', args);
		const teaching_building = args[0].target.value;
		const data = await nodejs('flippedclass', 'belong_area_find', { teaching_building });
		render(fd, data, P04, 'P05', 'lastin');
	},
	async eu02(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 change 区域', args);
		const belong_area = args[0].target.value;
		const data = await nodejs('flippedclass', 'floor_no_find', { belong_area });
		render(fd, data, P04, 'P06', 'lastin');
	},
	async eu03(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 click 查询', args);
		const teaching_building = (fd.data.node.querySelector('[data-feidao-id="d_teaching_building"]') as HTMLInputElement).value;
		const belong_area = (fd.data.node.querySelector('[data-feidao-id="d_belong_area"]') as HTMLInputElement).value;
		const floor_no = (fd.data.node.querySelector('[data-feidao-id="d_floor_no"]') as HTMLInputElement).value;
		const classroom_id = (fd.data.node.querySelector('[data-feidao-id="d_classroom_id"]') as HTMLInputElement).value;
		window.location.href = '';
	},
	async eu04(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 click 导入', args);
	},
	async eu05(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 click 导出', args);
		window.open(generate(fd.data.params, "./classroom_export.xlsx"));
	},
	async eu06(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 click 新增', args);
	},
	async eu07(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 click 编辑', args);
	},
	async eu08(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 click 删除', args);
		const _id = args[0].currentTarget.dataset.feidaoId;
		console.log(JSON.stringify({ rowdata: { _id } }));
		const res = await nodejs("flippedclass", "class_room_del", { rowdata: { _id } });
		if (res == 'success') {
			alert("操作成功");
			fd.fire('ec10');
		} else {
			alert("操作失败");
		}
	},
	async eu09(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 click 编辑实景图', args);
		const classroom_id = args[0].currentTarget.dataset.feidaoId;
		const house_name = args[0].currentTarget.dataset.feidaoName;
		const house_number = args[0].currentTarget.dataset.feidaoNumber;
		window.location.href = generate({ classroom_id, house_name, house_number }, './reality_picture.html');
	},
	async ec10(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-classroom-001 刷新', args);
		const page_no = 1;
		const page_size = 20;
		const query = {
			criteria: {},
			page_no,
			page_size,
			asc: ['fzno']
		};
		const res = await nodejs("flippedclass", "class_room_find", query);
		if (res.c == 0) {
			render(fd, '', P02, 'P01', 'inner');
		} else {
			render(fd, res.t, P01, 'P01', 'inner');
		}
		res['page_size'] = page_size;
		render(fd, res, P03, 'P03', 'inner');
		// const data_p04 = await nodejs("flippedclass", "teaching_building_find", {});
		// render(fd, data_p04, P04, 'P04','inner');
	}
};

const rules = {};

export default function main(url: string, query: any) {
	return init('zj-classroom-001', s, rules, feidao, daoke, url, query, { spaceid: 'flippedclass', systemid: '5991', refsystemid: '@refsystemid' });
}
